﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class CategoryEndPoints
    {
        private const string CategoriesUrl = "api/categories";
        private HttpClient client;

        public CategoryEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<CategoryViewModel>> GetAllCategoriesAsync()
        {
            List<CategoryViewModel> categories = new List<CategoryViewModel>();
            HttpResponseMessage response = await client.GetAsync(CategoriesUrl);
            if (response.IsSuccessStatusCode)
            {
                categories = await response.Content.ReadAsAsync<List<CategoryViewModel>>();
            }
            return categories;
        }

        public async Task<CategoryViewModel> GetCategoryAsync(int id)
        {
            CategoryViewModel category = null;
            HttpResponseMessage response = await client.GetAsync(CategoriesUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                category = await response.Content.ReadAsAsync<CategoryViewModel>();
            }
            return category;
        }

        public async Task CreateCategoryAsync(CategoryViewModel category)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(CategoriesUrl, category);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateCategoryAsync(CategoryViewModel category)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(CategoriesUrl, category);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteCategoryAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(CategoriesUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }

    }
}
